package com.kunyi.web.service.file.core.handel.Impl;

import com.kunyi.web.service.file.core.enums.KunyiRepositoryTypeEnum;
import javassist.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;

/**
 * @ClassName KunyiMinIoRepository
 * @Description 亚马逊S3(minIO)文件存储服务
 * @Author zhenguo.zhuang
 * @Date 2024/5/27 10:15
 * @Version 1.0
 */
@Slf4j
@Scope("prototype")
@Component("MinioRepository")
public class KunyiMinioRepository extends KunyiRepositoryAbstract {

    /**
     * 连接对象
     */
    private S3Client s3Client;


    @Override
    void loginInSpecific(String accessKeyId, String accessKeySecret,String endPoint) throws RuntimeException, URISyntaxException {
        AwsBasicCredentials awsCreds = AwsBasicCredentials.create(accessKeyId, accessKeySecret);
        Region region = Region.US_EAST_2;
        URI uri = new URI(endPoint);
        s3Client = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(awsCreds)).endpointOverride(uri).region(region).build();
    }

    @Override
    boolean createDirSpecific(String filePath) throws RuntimeException{
        byte[] bufffer = new byte[0];
        ByteBuffer byteBuffer= ByteBuffer.wrap(bufffer);
        s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(filePath)
                .build(), RequestBody.fromByteBuffer(byteBuffer));
        return true;
    }


    @Override
    void uploadSpecific(String filePath, String fileName, String appendFilePath, InputStream inputStream , Long length, MultipartFile multipartFile, File file) {
        s3Client.putObject(PutObjectRequest.builder().bucket(bucketName).key(appendFilePath)
                .build(), RequestBody.fromInputStream(inputStream, length));
    }


    @Override
    boolean deleteFileSpecific(String filePath) throws RuntimeException {
        DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(filePath).build();
        s3Client.deleteObject(deleteObjectRequest);
        return true;
    }

    @Override
    KunyiRepositoryTypeEnum type() {
        return KunyiRepositoryTypeEnum.MINIO;
    }


    @Override
    InputStream readStreamsSpecific(String filePath) throws RuntimeException{
        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                .bucket(bucketName)
                .key(filePath)
                .build();
        ResponseInputStream<GetObjectResponse> objectInputStream = s3Client.getObject(getObjectRequest);
        return objectInputStream;
    }

    @Override
    boolean isFileExistSpecific(String filePath) throws RuntimeException{
        HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
                .bucket(bucketName)
                .key(filePath)
                .build();
        // 不抛错就证明文件存在 抛NotFoundException意味着文件不存在
        try {
            s3Client.headObject(headObjectRequest);
        }catch (Exception e){
            if (e instanceof NotFoundException || e instanceof NoSuchKeyException){
                return false;
            }else {
                throw e;
            }
        }
        return true;
    }


}
